define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			needs: ["businessDetail", "businessCenter","businessDetailNodedetail"],

			business: Ember.computed.alias('controllers.businessDetail.model'),

			node: Ember.computed.alias('controllers.businessDetailNodedetail.model'),

			isRealtimeMode: Ember.computed.alias('controllers.businessDetail.isRealtimeMode'),

			sequenceChartDisplayMode: Ember.computed.alias('controllers.businessDetail.sequenceChartDisplayMode'),

			setup: function(model){
				this.set("isConfigIndicators", false);
				this.set("isCompareMode", false);
				this.set("model", model);
			},

			isRealtimeModeChange: Ember.observer("isRealtimeMode", "sequenceChartDisplayMode", function(){
				if(this.get("isRealtimeMode")||this.get("sequenceChartDisplayMode")=="detail") {
					return;
				}

				var indicators = this.get("indicators");
				var bsView = Broada.getView("businessSequenceView");

				indicators.forEach(function(item){
					var view = Broada.getView(item._chartId);
					if(bsView && view) {
						bsView.overviewChart.disConnect(view.chart);
					}
				});
			}),

			timesChange: Ember.observer("business.performances", function(){
				var perfs = this.get("business.performances");
				if( !(perfs && perfs.length > 0) ){
					return;
				}

				this.refreshData();
			}),

			indicators: Ember.computed("model", function(key, value){
				if(arguments.length == 1){
					var ctrl = this;
					var asynArr = Ember.A();
					asynArr.isLoading = true;
					setTimeout(function(){
						ctrl.requireIndicators(ctrl.model.protocol);
					});
					return asynArr;
				}else{
					Ember.A(value).forEach(function(item){
						item._checked = item.mode == 1;
						item._chartId = "node"+item.code+"View";
					});
					return value;
				}
			}),

			isCompareMode: false,

			modelChange: Ember.observer("model", "isCompareMode", function(){
				this.refreshData();
			}),

			indicatorCheckChange: Ember.observer("indicators.@each._checked", function(args){
				var checkIndictors = this.get("indicators").filterProperty('_checked', true),
					nodeId = this.get("model.id"),metricIds = [];
				checkIndictors.forEach(function(checkIndictor){
						metricIds.push(checkIndictor.metricId);
					});
				var isThreshold = overviewOrCenterSwitch == "business"?"business":"system";
				this.store.ajax("PUT", "node/indicators/"+nodeId+"/"+isThreshold, metricIds);
			}),

			requireIndicators: function(protocol){
				var ctrl = this,
					appId = this.get("business.id"),
					nodeId = this.get("model.id");
				this.set("indicators.isLoading", true);
				return this.store.ajax("GET", "node/indicators", {
					appId: appId,
					nodeId: nodeId,
					panelId:overviewOrCenterSwitch == "business"?"business":"system"
				})
				.then(function(response){
					ctrl.set("indicators", Ember.A(response.indicatorConfig));
					ctrl.set("indicators.isLoading", false);
					return response;
				});
			},

			requirePerformances: function() {
				var ctrl = this,
					appId = this.get("business.id"),
					nodeId = this.get("model.id"),
					appPfms = this.get("business.performances"),
					store = this.store,
					pfmsCompletion = ctrl.get("controllers.businessCenter").performancesCompletion;

				if( !(appPfms && appPfms.length > 0) ){

					return Ember.RSVP.reject("时序图没有数据");
				}

				var sDate = appPfms[0].timestamp;
				var sDate0, eDate0;
				sDate0 =  new Date(sDate);
				eDate0 =  new Date(+sDate0);
				eDate0.setHours(eDate0.getHours()+1);

				return store.ajax("GET", "performances", {
					appIds: appId,
					nodeIds: nodeId,
					startDate: +sDate0,
					endDate: +eDate0
				})
				.then(function(response){
					var data = pfmsCompletion(Ember.A(response[nodeId]||[]), sDate0, eDate0);
					ctrl.set("performances", data);
				});
			},

			requireCompareData: function() {
				var ctrl = this,
					appId = this.get("business.id"),
					nodeId = this.get("model.id"),
					appPfms = this.get("business.performances"),
					store = this.store,
					pfmsCompletion = ctrl.get("controllers.businessCenter").performancesCompletion;
				if( !(appPfms && appPfms.length > 0) ){
					return Ember.RSVP.reject("时序图没有数据");
				}

				var sDate = appPfms[0].timestamp;
				var sDate0, eDate0, sDate1, eDate1, sDate2, eDate2;

				sDate0 =  new Date(sDate);
				eDate0 =  new Date(+sDate0);
				eDate0.setHours(eDate0.getHours()+1);

				sDate1 = new Date(sDate);
				sDate1.setDate(sDate1.getDate()-1);
				eDate1 = new Date(+sDate1);
				eDate1.setHours(eDate1.getHours()+1);

				sDate2 = new Date(sDate);
				sDate2.setDate(sDate2.getDate()-7);
				eDate2 = new Date(+sDate2);
				eDate2.setHours(eDate2.getHours()+1);

				return Ember.RSVP.all([
					store.ajax("GET", "performances", {
						appIds: appId,
						nodeIds: nodeId,
						startDate: +sDate0,
						endDate: +eDate0
					}),
					store.ajax("GET", "performances", {
						appIds: appId,
						nodeIds: nodeId,
						startDate: +sDate1,
						endDate: +eDate1
					}),
					store.ajax("GET", "performances", {
						appIds: appId,
						nodeIds: nodeId,
						startDate: +sDate2,
						endDate: +eDate2
					})
				])
				.then(function(response){
					var nodePerformances = [
						pfmsCompletion(Ember.A(response[2][nodeId]||[]), sDate2, eDate2,1),
						pfmsCompletion(Ember.A(response[1][nodeId]||[]), sDate1, eDate1,2),
						pfmsCompletion(Ember.A(response[0][nodeId]||[]), sDate0, eDate0,3)
					];
					ctrl.set("nodePerformances", nodePerformances);
				});
			},

			refreshData: function(){
				if(this.get("isCompareMode")){
					this.requireCompareData();
					
				}else{
					this.requirePerformances();

				}
			},

			/*
				启动实时模式
			*/
			startRealTimeMode: function(){
				this.get("controllers.businessDetail").addRealTimeHook(this, "refreshData");
			},

			/*
				停止动实时模式
			*/
			stopRealTimeMode: function(){
				this.get("controllers.businessDetail").removeRealTimeHook(this, "refreshData");
			},

			actions: {
				toggleIndicatorsConfig: function(){
					this.set("isConfigIndicators", !this.get("isConfigIndicators"));
				},
				complateChoose:function(){
					var div = $(".tradesKeyUri")[0];
					if ("ActiveXObject" in window) {
					    // for IE
					    var r = document.body.createTextRange();
					    r.moveToElementText(div);
					    r.moveEnd("character"); 
					    r.select();
					} else {
					    // For others
					    var s = window.getSelection();
					    var r = document.createRange();
					    r.selectNode(div);
					    s.addRange(r);
					}
				}
			}
		});

		APM.BusinessDetailNodedetailOverviewController = Controller;

		return Controller;
	}
);	